<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>股道管理</title>
    <script type="text/javascript">
      var newTrackWin;
      var editTrackWin;
      var trackId;
      Ext.onReady(function(){
        //股道管理---------------------------------------------------------
        //生成有关股道的Grid
        var trackDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/track/getTracks'
          }),
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'track'
          },[
            {name:'id'},
            {name:'track_name'},
            {name:'device_id'},
            {name:'is_locked'}
          ]
        )
        });

        var trackSM = new Ext.grid.CheckboxSelectionModel();
        var trackCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          trackSM,
          {
            header: '是否锁定',
            dataIndex: 'is_locked',
            renderer: renderIsLucked,
            width: 70
          },
          {header:'股道名称',sortable:true,dataIndex:'track_name'},
          {header:'设备ID',sortable:true,dataIndex:'device_id'}
        ]);

        var trackGrid =  new Ext.grid.GridPanel({
          ds: trackDS,
          cm: trackCM,
          sm: trackSM,
          width: screenWidth-240,
          height: screenHeight-310,
          iconCls: 'icon-grid',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: trackDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',{
              text: '添加股道',
              width:50,
              iconCls: 'addItem',
              handler : function(){
                newTrackWin = new Ext.Window({
                  layout: 'fit',
                  width:267,
                  height:189,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加股道',
                  autoLoad: {
                    url: "/track/newTrack",
                    scripts: true
                  }
                });
                newTrackWin.on("destroy",function(){
                  trackDS.reload();
                });
                newTrackWin.resizable = false;
                newTrackWin.show();
              }
            },{
              text: '编辑股道',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = trackGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  //把表单添加到窗口中
                  trackId = records[0].get('id');
                  editTrackWin = new Ext.Window({
                    title: '编辑股道',
                    layout:'fit',
                    width:307,
                    height:189,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/track/editTrack?id=" + trackId,
                      scripts: true
                    }
                  });
                  editTrackWin.on("destroy",function(){
                    trackDS.reload();
                  });
                  editTrackWin.resizable = false;
                  editTrackWin.show();
                }
              }
            },
            {
              text:'锁定或锁定股道',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(trackGrid.getSelectionModel().hasSelection()){
                  var rs=trackGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var trackName='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    trackName+=data.track_name+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);

                  Ext.Ajax.request({
                    url:'/track/doLock',
                    method:'post',
                    waitMsg: '正在锁定或锁定股道...',
                    success:function(response,opts){
                      var data = Ext.util.JSON.decode(response.responseText);
                      if(data.success&&data.info=='success') {
                        trackDS.reload();
                      } else {
                        Ext.Msg.alert('提示信息', data.info);
                      }
                    },
                    params: {"id":keys}
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要锁定或锁定的股道');
                }
              }
            }]
        });
        trackGrid.render('trackList');
        trackDS.load();
      })
    </script>
  </head>
  <body>
    <div id="trackList"></div>
  </body>
</html>
